package org.djw.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.djw.basic.util.AjaxResult;
import org.djw.basic.util.PageList;
import org.djw.org.domain.Department;
import org.djw.org.query.DepartmentQuery;
import org.djw.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理接口
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /*
    查询一条,全部
    分页加高级查询
    新增或者修改
    删除
     */

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/list")
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @PostMapping("/page")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/batch")
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            departmentService.batch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }

    /**
     * 新增或者修改
     * @param department
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }

    /**
     * 查询上级部门树
     * @return
     */
    @PostMapping("/tree")
    public List<Department> findTree(){
        return departmentService.findTree();
    }
}
